package com.feng.online_xdclass.service.Impl;


import com.feng.online_xdclass.exception.XDexception;
import com.feng.online_xdclass.mapper.UserMapper;
import com.feng.online_xdclass.model.entity.User;
import com.feng.online_xdclass.service.UserService;
import com.feng.online_xdclass.utils.CommonUtils;
import com.feng.online_xdclass.utils.JWTUtils;
import com.feng.online_xdclass.utils.JsonDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public int save(Map<String, String> userInfo) {

        User user = parseToUser(userInfo);
        if( user != null){
           return userMapper.save(user);
        }else {
            return -1;
        }

    }

    @Override
    public String findByPhoneAndPwd(String phone, String pwd) {
        //先查询用户是否存在
        User user = userMapper.findByPhone(phone);
        if (user==null){
            throw new XDexception(-2,"该手机号不存在");
        }
        else{
            String shujukupwd = user.getPwd();
            String  secrectpwd=CommonUtils.MD5(pwd);
            if(!secrectpwd.equals(shujukupwd)){
                throw new XDexception(-3,"用户密码错误");
            }
            else{
                String s = JWTUtils.geneJsonWebToken(user);
                return s;
            }
        }
    }

    @Override
    public User  findByUserId(Integer id) {
        User user = userMapper.findByUserId(id);
        user.setPwd("");
        return user;
    }

    /**
     * 解析 user 对象
     * @param userInfo
     * @return
     */
    private User parseToUser(Map<String,String> userInfo) {

        if(userInfo.containsKey("phone") && userInfo.containsKey("pwd") && userInfo.containsKey("name")){
            String phone=userInfo.get("phone");
            User byPhone = userMapper.findByPhone(phone);
            if(byPhone==null) {
                User user = new User();
                user.setName(userInfo.get("name"));
                user.setHeadImg(getRandomImg());
                user.setCreateTime(new Date());
                user.setPhone(userInfo.get("phone"));
                String pwd = userInfo.get("pwd");
                //MD5加密
                user.setPwd(CommonUtils.MD5(pwd));
                return user;
            }
            else {
                return null;
            }


        }else {
            return null;
        }

    }

    /**
     * 放在CDN上的随机头像
     */
    private static final String [] headImg = {
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/11.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg(){
        int size =  headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }

}
